package com.github.phoenics.militia.bazookaFx.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.phoenics.militia.bazookaFx.BazookaFxApplication;
import com.github.phoenics.militia.bazookaFx.config.IconConfig;
import com.github.phoenics.militia.bazookaFx.domain.entity.App;
import com.github.phoenics.militia.bazookaFx.domain.entity.SchemaTablesView;
import com.github.phoenics.militia.bazookaFx.model.AppModel;
import com.github.phoenics.militia.bazookaFx.model.AppTableModel;
import com.github.phoenics.militia.bazookaFx.model.AppTreeModel;
import com.github.phoenics.militia.bazookaFx.service.AppService;
import com.github.phoenics.militia.bazookaFx.service.SchemaTablesViewService;
import com.github.phoenics.militia.bazookaFx.view.EditAppView;
import com.github.phoenics.militia.bazookaFx.view.SchemaTableView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
import javafx.stage.Modality;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author Phoenics Chow
 * on 2019年01月30日
 */
@FXMLController
public class MainLaunchController implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MainLaunchController.class);
    private Map<String,Consumer<AppModel>> funcMap=new HashMap<>();
    @Autowired
    AppService appService;
    @Autowired
    SchemaTableView schemaTableView;

    @FXML
    Tab functionTab;
    @FXML
    TreeView<AppTreeModel> projectTree;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        reloadAppTree();
        addFunc();
        projectTree.getSelectionModel().selectedItemProperty().addListener((ob,o,n)->{
            String funId=n.getValue().getFunId();
            Consumer<AppModel> funRun=funcMap.get(funId);
            if(funRun!=null){
                funRun.accept(n.getValue().getAppModel());
            }
        });
    }
    @FXML
    public void createApp(ActionEvent event){
        EditAppController.tagFire.setValue(EditAppController.tagFire.get()+1);
        BazookaFxApplication.showView(EditAppView.class, Modality.WINDOW_MODAL);
    }
    public void reloadAppTree(){
        List<App> list=appService.list();
        final TreeItem<AppTreeModel> dummyRoot = new TreeItem<>();
        projectTree.setShowRoot(false);
        projectTree.setRoot(dummyRoot);
        dummyRoot.getChildren().clear();
        list.forEach(p->{
            AppTreeModel appTreeModel=new AppTreeModel();
            appTreeModel.setName(p.getAppCnName());
            AppModel appModel=new AppModel();
            appModel.modelWarpper(p);
            appTreeModel.setAppModel(appModel);
            TreeItem<AppTreeModel> t=new TreeItem<>(appTreeModel, IconConfig.projectIcon());
            dummyRoot.getChildren().add(configProjectNode(t));
        });
    }
    private TreeItem<AppTreeModel> configProjectNode(TreeItem<AppTreeModel> treeItemPartent){
        TreeItem<AppTreeModel> brother1=configProjectFunctionNode("table","数据库表",treeItemPartent.getValue().getAppModel(),IconConfig.tableIcon());
        TreeItem<AppTreeModel> brother2=configProjectFunctionNode("nexus","表间关系",treeItemPartent.getValue().getAppModel(),IconConfig.raIcon());
        TreeItem<AppTreeModel> brother3=configProjectFunctionNode("xcode","生成代码",treeItemPartent.getValue().getAppModel(),IconConfig.xcodeIcon());
        treeItemPartent.getChildren().addAll(brother1,brother2,brother3);

        return treeItemPartent;
    }
    private TreeItem<AppTreeModel> configProjectFunctionNode(String funId, String name, AppModel object, ImageView icon){
        AppTreeModel tableRoot=new AppTreeModel();
        tableRoot.setFunId(funId);
        tableRoot.setName(name);
        tableRoot.setAppModel(object);
        return new TreeItem<>(tableRoot, icon);
    }
    private void addFunc(){
        funcMap.put("table",this::viewTable);
    }

    private void viewTable(AppModel app){
        functionTab.setContent(schemaTableView.getView());
        SchemaTableViewController.appModel.setValue(app);
        SchemaTableViewController.functionTabModel.setValue(functionTab);
    }
}
